var fakedata=require("../../static/js/fakedata")
var app=getApp();
Page({
    data: {
        commoditydetail:{
            name:"",
            price:"",
            address:"",
            url1:"",
            url2:"",
            introduction:"",
            publisherid:''
        },
        iconSize:'39.86rpx',
        canbuy:true,
        goodid:-1
    },
    cantbuy(){
        swan.showModal({
            title: '错误信息',
            content: '当前商品已经卖出！'
        });
    },
    buy(e){
        if(e.currentTarget.dataset.publisherid==app.globalData.userinfo.id){
            swan.showModal({
                title:"您不能买自己的商品"
            });
            return false;
        }
        if(app.globalData.islogin){
            let all_data={
                buyerid:app.globalData.userinfo.id,
                goodid:this.data.goodid
            };
            console.log(all_data);
            swan.request({
                url: fakedata.urls.buygood,
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Developer-Server-Domain': fakedata.host,
                    'X-SP-Accept-Encoding': 'base64'
                },
                method: 'POST',
                dataType: 'json',
                responseType: 'json',
                data: all_data,
                success: res => {
                    console.log(res);
                    if(res.statusCode==200){
                        if(res.data.data==1){
                            swan.showModal({
                                title: '提示',
                                content: '购买成功，请耐心等待卖家联系您'
                            });
                            this.setData({
                                canbuy:false
                            });
                        }else if(res.data.data==0){
                            swan.showModal({
                                title:"提示信息",
                                content:"当前商品已经被购买了"
                            });
                        }else{
                            swan.showModal({
                                title:"错误提示",
                                content:"当前商品不存在"
                            });
                        }
                    }else{
                        swan.showModal({
                            title:'服务器返回错误',
                            content:res.statusCode
                        });
                    }
                },
                fail: err => {
                    swan.showModal({
                        title: '错误信息',
                        content: JSON.stringify(err)
                    });
                }
            });
        }else{
            swan.showModal({
                title:"错误",
                content:"当前您还没有登录"
            });
            swan.navigateTo({
                url:'/pages/login/login'
            });
        }
    },
    addstar(){
        swan.request({
            url: fakedata.urls.stargood,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'json',
            data: {
                userid:app.globalData.userinfo.id,
                goodid:this.data.goodid
            },
            success: res => {
                console.log(res);
                swan.showModal({
                    title: '提示',
                    content: '收藏成功，可以在收藏列表查看相关信息'
                });
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
            }
        });
    },
    found(arr,value){
        for(let i=0;i<arr.length;i++){
            if(arr[i].goodId==value){
                return [arr[i]];
            }
        }
        return [];
    },
    onLoad(e){
        let goodid=e.goodid;
        this.setData({
            goodid:e.goodid
        });
        swan.request({
            url: fakedata.urls.getgoodbyid,
            header: {
                'Content-Type': 'application/json',
                'Developer-Server-Domain': fakedata.host,
                'X-SP-Accept-Encoding': 'base64'
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'json',
            data: {
                goodid:goodid
            },
            success: res => {
                console.log(res);
                if(res.statusCode==200){
                    let temp={
                        name:res.data.data.goodname,
                        price:res.data.data.price,
                        address:res.data.data.address,
                        url1:res.data.data.coverimg,
                        url2:res.data.data.descimg,
                        introduction:res.data.data.description,
                        publisherid:res.data.data.publisherid
                    }
                    this.setData({
                        commoditydetail:temp
                    });
                    console.log("执行成功");
                }else{
                    swan.showModal({
                        title:"错误",
                        content:res
                    });
                }
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
                console.log("后端出错");
            }
        });
    }
});